package com.abaaba.project2.service.wx.wxserviceimpl;

import com.abaaba.project2.bean.Category;
import com.abaaba.project2.bean.Goods;
import com.abaaba.project2.bean.GoodsExample;
import com.abaaba.project2.bean.bo.WxSearchGoodsBO;
import com.abaaba.project2.bean.vo.wx.WXSearchGoodsVO;
import com.abaaba.project2.mapper.CategoryMapper;
import com.abaaba.project2.mapper.GoodsMapper;
import com.abaaba.project2.service.wx.WxSearchGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author Hedgehog
 * @Date 2021/6/7 21:44
 * @Version 1.0
 */
@Service
public class WxSearchGoodsServiceImpl implements WxSearchGoodsService {


    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public WXSearchGoodsVO searchGoods(WxSearchGoodsBO wxSearchGoodsBO) {
        //需要进行两次查询，
        //1.第一次根据name模糊查询goods的表，得到goods的pojo的list
        //1.1 新建总vo，新建两个vo的list和一个count
        //2.拿到pojolist之后遍历获取到category的id，
        // 每次查询category表，获取category的pojo的list
        //3.进行拼接，有一个vo中有两个内部类，list类型的两个bean数据
        //4.在for循环中，新建两个vo，把这两个查询到的pojo转换成vo类型，并放入list中
        //5.每次for循环都有一个count++，把list放进总的vo中并放入countid

        //1
        String keyword = wxSearchGoodsBO.getKeyword();
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andNameLike("%"+keyword+"%");

        List<Goods> goods = goodsMapper.selectByExample(goodsExample);
        //1.1
        WXSearchGoodsVO wxSearchGoodsVO = new WXSearchGoodsVO();
        ArrayList<WXSearchGoodsVO.GoodsListBean> goodsListBeans = new ArrayList<>();
        ArrayList<WXSearchGoodsVO.FilterCategoryListBean> filterCategoryListBeans = new ArrayList<>();
        int count=0;

        for (Goods good : goods) {
            //2
            WXSearchGoodsVO.GoodsListBean goodsListBean = new WXSearchGoodsVO.GoodsListBean();
            WXSearchGoodsVO.FilterCategoryListBean filterCategoryListBean = new WXSearchGoodsVO.FilterCategoryListBean();


            //good中的
            Integer goodId = good.getId();
            String goodName = good.getName();
            String goodBrief = good.getBrief();
            String goodPicUrl = good.getPicUrl();
            Boolean goodIsNew = good.getIsNew();
            Boolean goodIsHot = good.getIsHot();
            BigDecimal goodCounterPrice = good.getCounterPrice();
            BigDecimal goodRetailPrice = good.getRetailPrice();
            goodsListBean.setId(goodId);
            goodsListBean.setName(goodName);
            goodsListBean.setBrief(goodBrief);
            goodsListBean.setPicUrl(goodPicUrl);
            goodsListBean.setNew(goodIsNew);
            goodsListBean.setHot(goodIsHot);
            //bigDecimal转化成int
            goodsListBean.setCounterPrice(goodCounterPrice.toBigInteger().intValue());
            goodsListBean.setRetailPrice(goodRetailPrice.toBigInteger().intValue());

            //3
            //category中的
            Integer categoryId = good.getCategoryId();
            Category category = categoryMapper.selectByPrimaryKey(categoryId);
            String categoryName = category.getName();
            String categoryKeywords = category.getKeywords();
            String categoryDesc = category.getDesc();
            Integer categoryPid = category.getPid();
            String categoryIconUrl = category.getIconUrl();
            String categoryPicUrl = category.getPicUrl();
            String categoryLevel = category.getLevel();
            Byte categorySortOrder = category.getSortOrder();
            Date categoryAddTime = category.getAddTime();
            Date categoryUpdateTime = category.getUpdateTime();
            Boolean categoryDeleted = category.getDeleted();

            filterCategoryListBean.setId(categoryId);
            filterCategoryListBean.setName(categoryName);
            filterCategoryListBean.setKeywords(categoryKeywords);
            filterCategoryListBean.setDesc(categoryDesc);
            filterCategoryListBean.setPid(categoryPid);
            filterCategoryListBean.setIconUrl(categoryIconUrl);
            filterCategoryListBean.setPicUrl(categoryPicUrl);
            filterCategoryListBean.setLevel(categoryLevel);
            filterCategoryListBean.setSortOrder(categorySortOrder);
            //时间转化为string
            filterCategoryListBean.setAddTime(categoryAddTime.toString());
            filterCategoryListBean.setUpdateTime(categoryUpdateTime.toString());
            filterCategoryListBean.setDeleted(categoryDeleted);

            //4
            goodsListBeans.add(goodsListBean);
            filterCategoryListBeans.add(filterCategoryListBean);
            count++;
        }
        //5
        wxSearchGoodsVO.setCount(count);
        wxSearchGoodsVO.setFilterCategoryList(filterCategoryListBeans);
        wxSearchGoodsVO.setGoodsList(goodsListBeans);

        return wxSearchGoodsVO;
    }
}
